package top.igotcha.chapter.chapter8.countdownLatch;

import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Gotcha
 * @date 2021/3/20
 * @describe
 */
public class CountdownLatchTest02 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(10);
        ExecutorService pool = Executors.newFixedThreadPool(10);
        Random r = new Random();
        String[] all = new String[10];
        System.out.println("等待所有玩家加载");
        for (int j = 0;j< 10;j++) {
            int k = j;
            pool.execute(()->{
                for (int i = 0; i <= 100; i++) {
                    try {
                        Thread.sleep(r.nextInt(100));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    all[k] = i + "%";
                    System.out.print("\r"+Arrays.toString(all));
                }
                latch.countDown();
            });
        }
        latch.await();
        System.out.println("\n游戏开始");
        pool.shutdown();
    }
}
